/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-30
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-06
 * @Description: 
 */
import 'package:hive/hive.dart';

part 'group.g.dart'; // 生成的文件，需运行 build_runner 生成

@HiveType(typeId: 0) // 0 代表存储的类型 ID，确保唯一
class Group extends HiveObject {
  @HiveField(0)
  String groupId;

  @HiveField(1)
  String groupName;

  @HiveField(2)
  String groupAvatar;

  @HiveField(3)
  String vertifyType;

  @HiveField(4)
  List<Member> members;

  Group({
    required this.groupId,
    required this.groupName,
    required this.groupAvatar,
    required this.vertifyType,
    required this.members,
  });

  factory Group.fromJson(Map<String, dynamic> json) {
    return Group(
      groupId: json['_id'] ?? '',
      groupName: json['groupName'] ?? '',
      groupAvatar: json['groupAvatar'] ?? '',
      vertifyType: json['vertifyType'] ?? '',
      members: (json['members'] as List<dynamic>?)
              ?.map((memberJson) => Member.fromJson(memberJson))
              .toList() ??
          [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': groupId,
      'groupName': groupName,
      'groupAvatar': groupAvatar,
      'vertifyType': vertifyType,
      'members': members,
    };
  }
}

// 成员类型
@HiveType(typeId: 1)
class Member extends HiveObject {
  @HiveField(0)
  String memberId;

  @HiveField(1)
  String groupNickname;

  @HiveField(2)
  String username;

  @HiveField(3)
  String nickname;

  @HiveField(4)
  String avatarUrl;

  @HiveField(5)
  String gender;

  Member({
    required this.memberId,
    required this.groupNickname,
    required this.username,
    required this.nickname,
    required this.avatarUrl,
    required this.gender,
  });

  factory Member.fromJson(Map<String, dynamic> json) {
    return Member(
      memberId: json['memberId'] ?? '',
      groupNickname: json['groupNickname'] ?? '',
      username: json['username'] ?? '',
      nickname: json['nickname'] ?? '',
      avatarUrl: json['avatarUrl'] ?? '',
      gender: json['gender'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'memberId': memberId,
      'groupNickname': groupNickname,
      'username': username,
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'gender': gender,
    };
  }
}
